import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {getToken} from '@utils/auth';

@Injectable({
  providedIn: 'root',
})
export class AuthService {
  constructor(private $http: HttpClient) {
  }

  login({email, password, remember}): Observable<any> {
    return this.$http.post(
      '/api/user/login',
      {email, password, remember},
      {
        responseType: 'json',
      }
    );
  }

  userInfo(): Observable<any> {
    const token = getToken();
    return this.$http.get('/api/user/userInfo', {
      headers: {
        Authorization: `Bearer ${token}`,
      },
      responseType: 'json',
    });
  }

  logout(): Observable<any> {
    return this.$http.post('/api/user/logout', {
      responseType: 'json',
    });
  }
}
